package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.NewSearchResultModel;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.SearchKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2018/3/23.
 */

public class NewSearchAdapter extends RecyclerView.Adapter<NewSearchAdapter.ViewHolder> {

    private final LayoutInflater inflater;
    private Context context;

    private String key;

    private OnItemClickListener onItemClickListener;

    private List<DataHolder> list;

    private static final int TITLE = 0;

    private static final int ANSWER = 1;

    private static final int COACH = 2;

    private static final int TEST = 3;

    private static final int LIVE = 4;

    private static final int INTER = 5;

    private static final int MORE = 6;

    private static final int DRIVER = 7;

    public NewSearchAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public interface OnItemClickListener {
        void VideoData(String title, double price, String id, String type, String img);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void setAnswerData(List<NewSearchResultModel.DataBean.ExpertQuUsersBean> expertQuUsers) {
        if (expertQuUsers != null && expertQuUsers.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("专家答疑");
            list.add(titleData);
            for (int i = 0; i < (expertQuUsers.size() > 3 ? 3 : expertQuUsers.size()); i++) {
                AnswerData answerData = new AnswerData();
                answerData.setData(expertQuUsers.get(i));
                list.add(answerData);
            }
            if (expertQuUsers.size() > 3) {
                MoreData moreData = new MoreData();
                moreData.setData(ANSWER);
                list.add(moreData);
            }

//            DriverData driverData = new DriverData();
//            list.add(driverData);
            notifyDataSetChanged();
        } else {
            return;
        }
    }

    public void setCoachData(List<NewSearchResultModel.DataBean.PolicysBean> policys) {
        if (policys != null && policys.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("政策解读");
            list.add(titleData);

            for (int i = 0; i < (policys.size() > 3 ? 3 : policys.size()); i++) {
                CoachData coachData = new CoachData();
                coachData.setData(policys.get(i));
                list.add(coachData);
            }
            if (policys.size() > 3) {
                MoreData moreData = new MoreData();
                moreData.setData(COACH);
                list.add(moreData);
            }

//            DriverData driverData = new DriverData();
//            list.add(driverData);
            notifyDataSetChanged();
        } else {
            return;
        }
    }

    public void setLiveData(List<NewSearchResultModel.DataBean.TinyVideosBean> tinyVideos) {
        if (tinyVideos != null && tinyVideos.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("财税课堂");
            list.add(titleData);

            for (int i = 0; i < (tinyVideos.size() > 3 ? 3 : tinyVideos.size()); i++) {
                LiveData liveData = new LiveData();
                liveData.setData(tinyVideos.get(i));
                list.add(liveData);
            }
            if (tinyVideos.size() > 3) {
                MoreData moreData = new MoreData();
                moreData.setData(LIVE);
                list.add(moreData);
            }

//            DriverData driverData = new DriverData();
//            list.add(driverData);
            notifyDataSetChanged();
        } else {
            return;
        }
    }

    public void setTestData(List<NewSearchResultModel.DataBean.ExamVideosBean> examVideos) {
        if (examVideos != null && examVideos.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("考前辅导");
            list.add(titleData);
            for (int i = 0; i < (examVideos.size() > 3 ? 3 : examVideos.size()); i++) {
                TestData testData = new TestData();
                testData.setData(examVideos.get(i));
                list.add(testData);
            }
            if (examVideos.size() > 3) {
                MoreData moreData = new MoreData();
                moreData.setData(TEST);
                list.add(moreData);
            }
//            DriverData driverData = new DriverData();
//            list.add(driverData);
            notifyDataSetChanged();
        } else {
            return;
        }
    }

    public void setInterViewData(List<NewSearchResultModel.DataBean.BigVideosBean> bigVideos) {
        if (bigVideos != null && bigVideos.size() > 0) {
            TitleData titleData = new TitleData();
            titleData.setData("大咖访谈");
            list.add(titleData);
            for (int i = 0; i < (bigVideos.size() > 3 ? 3 : bigVideos.size()); i++) {
                InterData interData = new InterData();
                interData.setData(bigVideos.get(i));
                list.add(interData);
            }
            if (bigVideos.size() > 3) {
                MoreData moreData = new MoreData();
                moreData.setData(INTER);
                list.add(moreData);
            }
//            DriverData driverData = new DriverData();
//            list.add(driverData);
            notifyDataSetChanged();
        } else {
            return;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public NewSearchAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TITLE:
                View titleView = inflater.inflate(R.layout.search_title_layout, null);
                return new TitleViewHolder(titleView);
            case ANSWER:
                View answerView = inflater.inflate(R.layout.search_content_layout, null);
                return new AnswerViewHolder(answerView);
            case COACH:
                View coachView = inflater.inflate(R.layout.search_content_layout, null);
                return new CoachViewHolder(coachView);
            case LIVE:
                View liveView = inflater.inflate(R.layout.search_content_layout, null);
                return new LiveViewHolder(liveView);
            case TEST:
                View testView = inflater.inflate(R.layout.search_content_layout, null);
                return new TestViewHolder(testView);
            case INTER:
                View interView = inflater.inflate(R.layout.search_content_layout, null);
                return new InterViewHolder(interView);
            case MORE:
                View moreView = inflater.inflate(R.layout.search_more_layout, parent, false);
                return new MoreViewHolder(moreView);
            case DRIVER:
                View driverView = inflater.inflate(R.layout.search_driver_layout, null);
                return new DriverViewHolder(driverView);

        }
        return null;
    }

    @Override
    public void onBindViewHolder(NewSearchAdapter.ViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            holder.setData(list.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    public abstract class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void setData(DataHolder data);
    }

    public abstract class DataHolder<T> {
        private T data;

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        abstract int getType();
    }


    public class TitleViewHolder extends ViewHolder {
        TextView title_tv;

        public TitleViewHolder(View itemView) {
            super(itemView);
            title_tv = itemView.findViewById(R.id.title);
        }

        @Override
        public void setData(DataHolder data) {
            String title = (String) data.getData();
            title_tv.getPaint().setFakeBoldText(true);
            title_tv.setText(title);
        }
    }

    public class AnswerViewHolder extends ViewHolder {

        TextView content_tv;

        public AnswerViewHolder(View itemView) {
            super(itemView);
            content_tv = itemView.findViewById(R.id.content);

        }

        @Override
        public void setData(DataHolder data) {
            final NewSearchResultModel.DataBean.ExpertQuUsersBean expertQuUsersBean = (NewSearchResultModel.DataBean.ExpertQuUsersBean) data.getData();
            final String content = expertQuUsersBean.getQuestion();
            String s = SearchKey.matcherSearchTitle(content, key);//关键字
            content_tv.setText(Html.fromHtml(s));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goExpertDetailActivity(context, expertQuUsersBean.getUuid());
                }
            });
        }
    }


    public class CoachViewHolder extends ViewHolder {
        TextView title;

        public CoachViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.content);
        }

        @Override
        public void setData(DataHolder data) {
            final NewSearchResultModel.DataBean.PolicysBean policysBean = (NewSearchResultModel.DataBean.PolicysBean) data.getData();
            String content = policysBean.getTitle();
            String s = SearchKey.matcherSearchTitle(content, key);//关键字
            title.setText(Html.fromHtml(s));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goCoachPolicyDetailActivity(context, policysBean.getUuid(), policysBean.getTitle(), policysBean.getCreateDate());
                }
            });
        }
    }

    public class LiveViewHolder extends ViewHolder {
        TextView title;

        public LiveViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.content);
        }

        @Override
        public void setData(DataHolder data) {
            final NewSearchResultModel.DataBean.TinyVideosBean tinyVideosBean = (NewSearchResultModel.DataBean.TinyVideosBean) data.getData();
            String content = tinyVideosBean.getTitle();
            String s = SearchKey.matcherSearchTitle(content, key);//关键字
            title.setText(Html.fromHtml(s));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.VideoData(tinyVideosBean.getTitle(), tinyVideosBean.getPrice(), tinyVideosBean.getId(), tinyVideosBean.getType(), tinyVideosBean.getImage());
                    }
                }
            });
        }
    }

    public class TestViewHolder extends ViewHolder {
        TextView title;

        public TestViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.content);
        }

        @Override
        public void setData(DataHolder data) {
            final NewSearchResultModel.DataBean.ExamVideosBean examVideosBean = (NewSearchResultModel.DataBean.ExamVideosBean) data.getData();
            String content = examVideosBean.getTitle();
            String s = SearchKey.matcherSearchTitle(content, key);//关键字
            title.setText(Html.fromHtml(s));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.VideoData(examVideosBean.getTitle(), examVideosBean.getPrice(), examVideosBean.getId(), examVideosBean.getType(), examVideosBean.getImage());
                    }
                }
            });
        }
    }

    public class InterViewHolder extends ViewHolder {
        TextView title;

        public InterViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.content);
        }

        @Override
        public void setData(DataHolder data) {
            final NewSearchResultModel.DataBean.BigVideosBean bigVideosBean = (NewSearchResultModel.DataBean.BigVideosBean) data.getData();
            String content = bigVideosBean.getTitle();
            String s = SearchKey.matcherSearchTitle(content, key);//关键字
            title.setText(Html.fromHtml(s));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.VideoData(bigVideosBean.getTitle(), bigVideosBean.getPrice(), bigVideosBean.getId(), bigVideosBean.getType(), bigVideosBean.getImage());
                    }
                }
            });
        }
    }

    public class MoreViewHolder extends ViewHolder {

        TextView more;

        public MoreViewHolder(View itemView) {
            super(itemView);
            more = itemView.findViewById(R.id.more);
            Drawable drawable = context.getResources().getDrawable(R.drawable.go);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getIntrinsicHeight());
            more.setCompoundDrawables(null, null, drawable, null);
        }

        @Override
        public void setData(DataHolder data) {
            final int type = (int) data.getData();
            switch (type) {
                case ANSWER:
                    more.setText("查看更多专家答疑");
                    break;
                case LIVE:
                    more.setText("查看更多财税课堂");
                    break;
                case COACH:
                    more.setText("查看更多政策解读");
                    break;
                case TEST:
                    more.setText("查看更多考前辅导");
                    break;
                case INTER:
                    more.setText("查看更多大咖访谈");
                    break;
            }

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (type) {
                        case ANSWER:
                            IntentManager.goMoreResultActivity(context, key, "专家答疑", "answer");
                            break;
                        case LIVE:
                            IntentManager.goMoreResultActivity(context, key, "财税课堂", "live");
                            break;
                        case COACH:
                            IntentManager.goMoreResultActivity(context, key, "政策解读", "coach");
                            break;
                        case TEST:
                            IntentManager.goMoreResultActivity(context, key, "考前辅导", "test");
                            break;
                        case INTER:
                            IntentManager.goMoreResultActivity(context, key, "大咖访谈", "inter");
                            break;
                    }
                }
            });


        }
    }

    public class DriverViewHolder extends ViewHolder {
        public DriverViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData(DataHolder data) {

        }
    }


    public class TitleData extends DataHolder<String> {

        @Override
        int getType() {
            return TITLE;
        }
    }

    public class AnswerData extends DataHolder<NewSearchResultModel.DataBean.ExpertQuUsersBean> {

        @Override
        int getType() {
            return ANSWER;
        }
    }

    public class CoachData extends DataHolder<NewSearchResultModel.DataBean.PolicysBean> {

        @Override
        int getType() {
            return COACH;
        }
    }

    public class LiveData extends DataHolder<NewSearchResultModel.DataBean.TinyVideosBean> {

        @Override
        int getType() {
            return LIVE;
        }
    }

    public class TestData extends DataHolder<NewSearchResultModel.DataBean.ExamVideosBean> {

        @Override
        int getType() {
            return TEST;
        }
    }

    public class InterData extends DataHolder<NewSearchResultModel.DataBean.BigVideosBean> {

        @Override
        int getType() {
            return INTER;
        }
    }

    public class MoreData extends DataHolder {

        @Override
        int getType() {
            return MORE;
        }
    }

    public class DriverData extends DataHolder {

        @Override
        int getType() {
            return DRIVER;
        }
    }

    public void clearAdapter() {
        if (this.list != null)
            list.clear();
        notifyDataSetChanged();
    }
}
